package com.party.admin.biz.copartner;

import com.party.common.utils.BigDecimalUtils;
import com.party.common.utils.StringUtils;
import com.party.core.model.copartner.BrokerageItem;
import com.party.core.model.member.MemberCopartnerCount;
import com.party.core.model.wallet.Withdrawals;
import com.party.core.service.copartner.IBrokerageItemService;
import com.party.core.service.crowdfund.IProjectService;
import com.party.core.service.member.IMemberActService;
import com.party.core.service.member.IMemberCopartnerCountService;
import com.party.core.service.member.IMemberService;
import com.party.core.service.order.IOrderFormService;
import com.party.core.service.wallet.biz.WithdrawalsBizService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 合作商统计总计业务服务
 * <p>
 * Created by wei.li.
 *
 * @date 2018/1/17 0017
 * @time 17:08
 */
@Service
public class MemberCopartnerCountBizService {

    @Autowired
    private IMemberService memberService;

    @Autowired
    private IMemberActService memberActService;

    @Autowired
    private IOrderFormService orderFormService;

    @Autowired
    private IProjectService projectService;

    @Autowired
    private IBrokerageItemService brokerageItemService;

    @Autowired
    private IMemberCopartnerCountService memberCopartnerCountService;

    @Autowired
    private WithdrawalsBizService withdrawalsBizService;

    Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * 跟新今天的数据
     */
    public void todayDate() {
        List<String> list = memberService.getAllChannelDis();

        for (String mId : list) {

            try {
                MemberCopartnerCount memberCopartnerCount = memberCopartnerCountService.getSet(mId);

                //报名
                Integer applyNum = memberActService.disCount(mId, null, null);

                // 订单数 累计资金
                Map<String, Object> orderData = orderFormService.disCount(mId, null, null);
                Integer orderNum = (Integer) orderData.get("orderNum");
                Double totalMoney = (Double) orderData.get("totalMoney");

                //众筹成功数
                Integer successNum = projectService.disCount(mId, null, null);

                //累计佣金
                Double totalBrokerage = brokerageItemService.getMyBrokerage(mId, null, null, null, true);

                // 我的佣金
                Double myBrokerage = brokerageItemService.getMyBrokerage(mId, null, null, null, false);

                // 未结算
                Double notBalance = brokerageItemService.getMyBrokerage(mId, BrokerageItem.STATUS_NOT_BROKERAGE, null, null, true);

                // 可提现
                Double balance = brokerageItemService.getMyBrokerage(mId, BrokerageItem.STATUS_CAN_BROKERAGE, null, null, true);

                // 累计提现佣金
                Double withdrawalTotal = withdrawalsBizService.getWithdrawalTotal(mId, Withdrawals.TYPE_BROKERAGE,null);

                // 可提现佣金
                double canWithdrawal = BigDecimalUtils.sub(balance, withdrawalTotal);

                memberCopartnerCount.setApplyNum(applyNum);
                memberCopartnerCount.setOrderNum(orderNum);
                memberCopartnerCount.setSuccessNum(successNum);
                memberCopartnerCount.setTotalMoney(totalMoney);
                memberCopartnerCount.setTotalBrokerage(totalBrokerage);
                memberCopartnerCount.setMyBrokerage(myBrokerage);
                memberCopartnerCount.setMemberId(mId);
                memberCopartnerCount.setNotBalance(notBalance);
                memberCopartnerCount.setCanWithdrawal(BigDecimalUtils.round(canWithdrawal, 2));

                if (StringUtils.isEmpty(memberCopartnerCount.getId())) {
                    memberCopartnerCountService.insert(memberCopartnerCount);
                } else {
                    memberCopartnerCountService.update(memberCopartnerCount);
                }
            } catch (Exception e) {
                logger.error("刷新合伙人count数据异常：memberId-{}", e, mId);
                continue;
            }
        }
    }
}
